package com.ddwl.shop.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.QueryShopCategoryBo;
import com.ddwl.schema.bo.shop.ShopCategoryBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.shop.ShopCategoryVo;
import com.ddwl.shop.dao.ShopCategoryDao;
import com.ddwl.shop.dao.model.ShopCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 门店审核
 *
 * @author wenbin
 * @version 1.0.0.RELEASE
 * @date 2020/3/5 11:00 上午
 */
@Service
@RequiredArgsConstructor
public class ShopCategoryService {

    private final ShopCategoryDao shopCategoryDao;

    @Transactional(rollbackFor = Throwable.class)
    public void createShopCategory(ShopCategoryBo bo, String userId) {
        ShopCategory shopCategory = shopCategoryDao.queryShopCategoryCode(bo.getShopCategoryCode());
        if (shopCategory != null) {
            throw new CommonException("分类编号已被使用");
        }
        shopCategory = BeanConverUtils.convertBean(bo, ShopCategory.class);
        shopCategory.setEffect(BaseEnum.StatusEnum.DISABLE.getCode());
        shopCategory.setCreatedUserId(userId);
        shopCategoryDao.insert(shopCategory);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void updateShopCategory(ShopCategoryBo bo, String userId) {
        ParamException.isBlank(bo.getId(), "id为空");
        ShopCategory shopCategory = shopCategoryDao.queryShopCategoryCode(bo.getShopCategoryCode());
        if (shopCategory != null && !bo.getId().equals(shopCategory.getId())) {
            throw new CommonException("分类编号已被使用");
        }
        shopCategory = BeanConverUtils.convertBean(bo, ShopCategory.class);
        shopCategory.setModifiedUserId(userId);
        shopCategoryDao.updateByPrimaryKeySelective(shopCategory);
    }

    public PageVo<ShopCategoryVo> page(PageBo<QueryShopCategoryBo> boPageBo) {
        PageHelper.startPage(boPageBo.getPage(), boPageBo.getPageSize(), MySqlUtil.orderBy(boPageBo.getSort(), boPageBo.isAsc()));
        PageInfo<ShopCategory> info = new PageInfo(shopCategoryDao.queryShopCategoryList(boPageBo.getParam()));
        List<ShopCategoryVo> list = BeanConverUtils.convertBeanList(info.getList(), ShopCategoryVo.class);
        return new PageVo<>(info.getTotal(), boPageBo.getPage(), boPageBo.getPageSize(), list);
    }

    public List<ShopCategoryVo> getShopCategoryList(QueryShopCategoryBo boPageBo) {
        boPageBo.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        List<ShopCategoryVo> list = BeanConverUtils.convertBeanList(shopCategoryDao.queryShopCategoryList(boPageBo), ShopCategoryVo.class);
        return list;
    }

    public ShopCategoryVo getById(String shopCategoryId) {
        ParamException.isNull(shopCategoryId, "分类id为空");
        ShopCategoryVo shopCategoryVo = BeanConverUtils.convertBean(shopCategoryDao.queryShopCategory(shopCategoryId), ShopCategoryVo.class);
        return shopCategoryVo;
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeShopCategory(String id, Integer effect, String userIdNew) {
        ShopCategory shopCategory = new ShopCategory();
        shopCategory.setId(id);
        shopCategory.setModifiedUserId(userIdNew);
        shopCategory.setEffect(effect);
        shopCategoryDao.updateByPrimaryKeySelective(shopCategory);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteShopCategory(String id, String userId) {
        shopCategoryDao.deleteById(id, userId);
    }

    public List<ShopCategoryVo> getShopCategory(String categoryCode) {
        List<String> list = Arrays.asList(categoryCode.split(","));
        QueryShopCategoryBo bo = new QueryShopCategoryBo();
        bo.setCategoryCodeList(list);
        return BeanConverUtils.convertBeanList(shopCategoryDao.queryShopCategoryList(bo), ShopCategoryVo.class);
    }

    public List<ShopCategoryVo> getByCodeList(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        QueryShopCategoryBo bo = new QueryShopCategoryBo();
        bo.setCategoryCodeList(list);
        return BeanConverUtils.convertBeanList(shopCategoryDao.queryShopCategoryList(bo), ShopCategoryVo.class);
    }
}
